﻿using ESCDB.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace ESC.Designer.Resources
{
    public static class Utils
    {
        public static T DeepCopyByReflect<T>(T obj)
        {
            //如果是字符串或值类型则直接返回
            if (obj is string || obj.GetType().IsValueType) return obj;
            object retval = Activator.CreateInstance(obj.GetType());
            FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
            foreach (FieldInfo field in fields)
            {
                try { field.SetValue(retval, DeepCopyByReflect(field.GetValue(obj))); }
                catch { }
            }
            return (T)retval;
        }

     

        

        public static void SpitBz(object obj, string bz, string lsl, string usl)
        {
            string[] ss = bz.Split('~');
            Type t = obj.GetType();
            if (ss.Length >= 1)
            {
                System.Reflection.PropertyInfo property = obj.GetType().GetProperty(lsl);
                property.SetValue(obj, string.IsNullOrEmpty(ss[0]) ? null : ChangeType(ss[0], property.PropertyType), null);
            }
            if (ss.Length >= 2)
            {
                System.Reflection.PropertyInfo property = obj.GetType().GetProperty(usl);
                property.SetValue(obj, string.IsNullOrEmpty(ss[1]) ? null : ChangeType(ss[1], property.PropertyType), null);
            }
        }

        static public object ChangeType(object value, Type type)
        {
            if (value == null && type.IsGenericType) return Activator.CreateInstance(type);
            if (value == null) return null;
            if (type == value.GetType()) return value;
            if (type.IsEnum)
            {
                if (value is string)
                    return Enum.Parse(type, value as string);
                else
                    return Enum.ToObject(type, value);
            }
            if (!type.IsInterface && type.IsGenericType)
            {
                Type innerType = type.GetGenericArguments()[0];
                object innerValue = ChangeType(value, innerType);
                return Activator.CreateInstance(type, new object[] { innerValue });
            }
            if (value is string && type == typeof(Guid)) return new Guid(value as string);
            if (value is string && type == typeof(Version)) return new Version(value as string);
            if (!(value is IConvertible)) return value;
            return Convert.ChangeType(value, type);
        }


        public static string GetK(string fk, string partno, string ctype)
        {
            try
            {
                List<string> ls = new List<string> { "J", "K", "L", "6", "M", "N", "7", "O", "P", "Q", "R", "S", "T", "U", "3", "V", "W" };
                if (!ls.Contains(partno.Substring(14, 1)) || partno.Substring(15, 2).CompareTo("25") < 0)
                {
                    return "";
                }
                if (ctype == "POL")
                    return "";
                else if (ctype == "SNP")
                {
                    decimal fkd = Convert.ToDecimal(fk);
                    if (partno.Substring(15, 2).CompareTo("25") < 0)
                    {
                        return fkd.ToString("0.###");
                    }
                    if (partno.Substring(17, 1) == "4" && partno.Substring(6, 1) == "M")
                    {
                        return (fkd - 0.04m).ToString("0.###");
                    }
                    return (fkd - 0.02m).ToString("0.###");
                }
                else
                {
                    decimal fkd = Convert.ToDecimal(fk);
                    //KSH825L400D2S5812K
                    if (partno.Substring(17, 1) == "4")
                    {
                        return (fkd - 0.02m).ToString("0.###");
                    }
                    if (partno.Substring(0, 3) == "DSM" || partno.Substring(0, 3) == "KSM")
                    {
                        if (partno.Substring(6, 1) == "M")
                        {
                            return (fkd - 0.02m).ToString("0.###");
                        }
                        return fkd.ToString("0.###");
                    }
                    List<string> strs = new List<string>() { "SC", "SY", "SJ", "SZ", "ST", "TA", "TB", "TC", "AK", "AH", "LZ", "LT", "LU", "LY", "LW", "SX" };
                    if (strs.Contains(partno.Substring(1, 2)))
                    {
                        if (partno.Substring(6, 1) == "M")
                        {
                            return (fkd - 0.02m).ToString("0.###");
                        }
                        return fkd.ToString("0.###");
                    }
                    if (partno.Substring(6, 1) == "M" || partno.Substring(6, 1) == "G")
                    {
                        return (fkd - 0.02m).ToString("0.###");
                    }
                    if (partno.Substring(6, 1) == "E" || partno.Substring(6, 1) == "L")
                    {
                        return (fkd - 0.02m).ToString("0.###");
                    }
                    if (partno.Substring(7, 3).CompareTo("101") <= 0)
                    {
                        return (fkd - 0.02m).ToString("0.###");
                    }
                    else
                    {
                        return (fkd - 0.02m).ToString("0.###");
                    }
                }
            }
            catch
            {
            }
            return "";
        }
    }
}

public static class ConvertionExtensions
{
    public static T? ConvertTo<T>(this IConvertible convertibleValue) where T : struct
    {
        if (null == convertibleValue) return null;
        return (T?)Convert.ChangeType(convertibleValue, typeof(T));
    }
}
